<?php

namespace App\Model;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Counter extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'counter';
    protected $table_cate = 'counter_cate';
    protected $gift_table = 'gift_library';
    protected $box_table = 'box';
    protected $pearl_gift_table = 'gift_pearl_library';
    protected $counter_name = [1 => 'CBD', 2 => '小屋', 3 => '私信', '3D场景', '扩列' ,6=>'多人匿名', 7 => '密屋',8 => '测试货架'];
    protected $goods_type = [1 => '钻石礼物', 2 => '小屋礼物', 3 => '定时礼物', 4 => '免费礼物', 5 => '宝箱', 6 => '技能礼物' ,7=>'涂鸦礼物'];//1. 2. 3. 4. 5.


    public function getList($input = [])
    {
        $limit = $input['limit'] ?? 10;
        $goods_id = $input['goods_id'] ?? '';
        $counter_cate_id = $input['counter_cate_id'] ?? '';
        $where = [];
        if ($goods_id) {
            $where['goods_id'] = $goods_id;
        }
        if ($counter_cate_id) {
            $where[$this->table.'.counter_cate_id'] = $counter_cate_id;
        }
        $where[$this->table.'.counter_id'] = $input['counter_id'] ?? 1;
        $res = DB::connection($this->connection)->table($this->table)
            ->where($where)
            ->leftJoin('counter_cate', 'counter_cate.counter_cate_id', '=', $this->table . '.counter_cate_id')
            ->orderBy('order', 'asc')
            ->select($this->table.'.*',$this->table_cate.'.name as counter_cate_name')
            ->paginate($limit);
        $res = $this->objToArray($res);
        foreach ($res['data'] as &$v) {
            $where = [];
            if ($v['goods_type'] == 2) {
                $where['gift_id'] = $v['goods_id'];
                $api_table = $this->pearl_gift_table;
            } else if ($v['goods_type'] == 5) {
                $where['id'] = $v['goods_id'];
                $api_table = $this->box_table;
            } else {
                $where['gift_id'] = $v['goods_id'];
                $api_table = $this->gift_table;
            }
            $v['gift_info'] = $this->objToArray(DB::connection($this->connection)->table($api_table)->where($where)->first());
            $v['counter_name'] = $this->counter_name[$v['counter_id']] ?? "";
            $v['goods_type_name'] = $this->goods_type[$v['goods_type']]??0;
            $v['start_time'] = date($v['start_time']);
            $v['end_time'] = date($v['end_time']);
        }
        return $res;
    }


    public function delCounter($input = [])
    {
        $data['id'] = $input['id'] ?? 0;
        return curl_request(getKkaudioApiUrl() . '/inner/counter/remove', $data, 'urlencoded', 'GET');
    }

    /**
     * 创建或更新
     * prop_id 存在则更新，不存在则新增
     * @param $data
     * @return mixed
     */
    public function createOrUpdateData($data)
    {
        $data['start_time'] = isset($data['scope_time'][0]) ? strtotime($data['scope_time'][0]) : 0;
        $data['end_time'] = isset($data['scope_time'][1]) ? strtotime($data['scope_time'][1]) :0;
        unset($data['scope_time']);
        return curl_request(getKkaudioApiUrl() . '/inner/counter/upsert', $data, 'json', 'POST');
    }


    public function getCate($input)
    {
        $where['counter_id'] = $input['counter_id'] ?? 1;
        $res = DB::connection($this->connection)->table($this->table_cate)->where($where)->get();
        return $this->objToArray($res);
    }

    public function createCate($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/counter/cate/upsert', $data, 'json', 'POST');
    }

    public function delCate($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/counter/cate/remove', $data, 'urlencoded', 'GET');
    }

    public function createOrUpdateCounterNewPlay(array $data = []) {
        return curl_request(getKkaudioApiUrl() . '/inner/counter/new_play', $data, 'json', 'POST');
    }

    public function getCounterNewPlay(int $counterId) {
        $res = Db::connection('api')->table('counter_new_play')->where('counter_id', $counterId)->first();
        return $this->objToArray($res);
    }
}
